package com.example.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.Dto.DishDto;
import com.example.ruiji.common.Result;
import com.example.ruiji.entity.Dish;
import com.example.ruiji.entity.DishFlavor;
import com.example.ruiji.mapper.CategoryMapper;
import com.example.ruiji.mapper.DishFlavorMapper;
import com.example.ruiji.mapper.DishMapper;
import com.example.ruiji.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Result<Page<DishDto>> Page(Integer page, Integer pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null, Dish::getName, name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        Page<Dish> dishPage = dishMapper.selectPage(pageInfo, lambdaQueryWrapper);
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<DishDto> dishDtoList = new ArrayList<>();
        dishPage.getRecords().forEach(record -> {
            DishDto dishDto = new DishDto();
            Long categoryId = record.getCategoryId();
            String categoryName = categoryMapper.selectById(categoryId).getName();
            BeanUtils.copyProperties(record, dishDto);
            dishDto.setCategoryName(categoryName);
            dishDtoList.add(dishDto);
        });
        dishDtoPage.setRecords(dishDtoList);
        return Result.success(dishDtoPage);
    }

    @Override
    @Transactional
    public Result<String> add(DishDto dishDto) {
         dishMapper.insert(dishDto);
         dishDto.getFlavors().forEach(dishFlavor -> {
             dishFlavor.setDishId(dishDto.getId());
             dishFlavorMapper.insert(dishFlavor);
         });
        return Result.success("新增成功");
    }

    @Override
    public Result<DishDto> getById(Long id) {
        Dish dish = dishMapper.selectById(id);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(lambdaQueryWrapper);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavors);
        return Result.success(dishDto);
    }

    @Override
    @Transactional
    public Result<String> update(DishDto dishDto) {
        dishMapper.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        //先删除
        dishFlavorMapper.delete(lambdaQueryWrapper);
        //后添加
        dishDto.getFlavors().forEach(flavor -> {
            flavor.setDishId(dishDto.getId());
            dishFlavorMapper.insert(flavor);
        });
        return Result.success("修改成功");
    }

    @Override
    public Result<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(!Objects.isNull(dish.getCategoryId()), Dish::getCategoryId, dish.getCategoryId());
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishMapper.selectList(dishLambdaQueryWrapper);
        List<DishDto> dishDtoList = new ArrayList<>();
        dishes.forEach(record -> {
            DishDto dishDto = new DishDto();
            Long categoryId = record.getCategoryId();
            String categoryName = categoryMapper.selectById(categoryId).getName();
            BeanUtils.copyProperties(record, dishDto);
            dishDto.setCategoryName(categoryName);
            Long dishId = record.getId();
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);

            dishDtoList.add(dishDto);
        });
        return Result.success(dishDtoList);
    }
}
